'''
Created on 21-Apr-2013

@author: mahesh
'''
from collections import Counter

class Stats():
    '''
    Calculates the stats for msgs
    '''

    def __init__(self, msgs, name):
        '''
        Constructor
        '''
        if len(msgs) > 0:
            self.extract_stats(msgs)
        
        self.__name = "../Output/" + name + ".csv"

    def dump_stats(self):
        statfd = open(self.__name, "w+")
        statfd.write("Number of Messages,\t\t\t {}\n".format(self.__num_msgs))
        statfd.write("Number of Words,\t\t\t {}\n".format(self.__num_words))
        statfd.write("Number of Unique Words,\t\t\t {}\n".format(self.__num_unique_words))
        statfd.write("Average size of Messages,\t\t {}\n".format(self.average_msg_size()))
        statfd.write("Min size of Messages,\t\t {}\n".format(self.min_msg_size()))
        statfd.write("Max size of Messages,\t\t {}\n".format(self.max_msg_size()))
        statfd.write("Most frequent top 10 words,\t\t {}\n".format(self.get_word_freq().most_common(10)))        
        
        statfd.flush()
        statfd.close()
        

    def print_stats(self):
        
        print '-' * 80
        print self.__name
        print 'Number of Messages,\t\t\t', self.__num_msgs
        print 'Number of Words,\t\t\t', self.__num_words
        print 'Number of Unique Words,\t\t\t', self.__num_unique_words
        print 'Average size of Messages,\t\t', self.average_msg_size()
        print 'Min size of Messages,\t\t', self.min_msg_size()
        print 'Max size of Messages,\t\t', self.max_msg_size()
        print 'Most frequent top 10 words,\t\t', self.get_word_freq()
        print '-' * 80
       
    def average_msg_size(self):
        return sum(self.__size_of_all_msgs)/self.__num_msgs

    def max_msg_size(self):
        return max(self.__size_of_all_msgs)
    
    def min_msg_size(self):
        return min(self.__size_of_all_msgs)
    
    def get_word_freq(self):
        word_freq = Counter(self.all_words_list)
        return word_freq
        
    def get_num_msgs(self):
        return self.__num_msgs

    def get_num_words(self):
        return self.__num_words

    def get_all_words_str(self):
        return self.__all_words_str

    def get_all_words_list(self):
        return self.__all_words_list

    def get_unique_words(self):
        return self.__unique_words

    def get_num_unique_words(self):
        return self.__num_unique_words


    def set_num_msgs(self, value):
        self.__num_msgs = value


    def set_num_words(self, value):
        self.__num_words = value


    def set_all_words_str(self, value):
        self.__all_words_str = value


    def set_all_words_list(self, value):
        self.__all_words_list = value


    def set_unique_words(self, value):
        self.__unique_words = value


    def set_num_unique_words(self, value):
        self.__num_unique_words = value

    def extract_stats(self, msgs):
        
        self.__num_msgs = len(msgs)                
        self.__num_words = 0
                 
        self.__size_of_all_msgs = list()
        self.__all_words_str   = ""
        self.__all_words_list  = list()
        
        for msg in msgs:
            
            words_in_msg = msg.split(" ")
            self.__size_of_all_msgs.append(len(words_in_msg))

            
            for word in words_in_msg:
                self.__num_words += 1
                self.__all_words_str += word + " "
                self.__all_words_list.append(word)
        
        self.__unique_words = set(self.__all_words_list)
        self.__num_unique_words = len(self.__unique_words)
        
    def get_size_of_all_msgs(self):
        return self.__size_of_all_msgs
    
    num_msgs = property(get_num_msgs, set_num_msgs, None, None)
    num_words = property(get_num_words, set_num_words, None, None)
    all_words_str = property(get_all_words_str, set_all_words_str, None, None)
    all_words_list = property(get_all_words_list, set_all_words_list, None, None)
    unique_words = property(get_unique_words, set_unique_words, None, None)
    num_unique_words = property(get_num_unique_words, set_num_unique_words, None, None)
        
        
if __name__ == '__main__':
    
    msgs = ['i am fine', 'how are you', 'good morning to you']
    stats = Stats(msgs, "../Output/Exp01.csv")
    stats.print_stats()
    stats.dump_stats()
    